<script setup lang="ts">
import type { CommentInfo } from '@/types/comment'
import { ElNotification, ElMessageBox } from 'element-plus'
import { ref, onMounted } from 'vue'
import { getCommentInfo, delComment } from '@/api/comment'
import { formatDateFull } from '@/utils/format'

// 获取评论数据
const commentData = ref<CommentInfo[]>([])
const loading = ref(false)
// 分页
const total = ref(0)
const params = ref({
  // 当前页
  page: 1,
  // 当前生效的每页条数
  pageSize: 10,
  // 关键字
  key: ''
})
const getComment = async () => {
  loading.value = true
  const result = await getCommentInfo(params)
  commentData.value = result.data
  // @ts-ignore
  total.value = result.total
  commentData.value.map(
    (item: CommentInfo) => (item.time = formatDateFull(item.time))
  )
  loading.value = false
}
const handleSizeChange = (size: number) => {
  params.value.page = 1
  params.value.pageSize = size
  getComment()
}
const handleCurrentChange = (value: number) => {
  params.value.page = value
  getComment()
}
const searchBtn = () => {
  getComment()
}
const resetBtn = () => {
  params.value.key = ''
  getComment()
}

// 是否显示抽屉
const commentDetail = ref<CommentInfo>({} as CommentInfo)
const isShowCommentDialog = ref(false)
const selectDetail = (row: CommentInfo) => {
  isShowCommentDialog.value = true
  commentDetail.value = row
}
// 删除评论
const delBtn = async (id: number) => {
  await ElMessageBox.confirm('你确定要删除该评论吗？', '温馨提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
  await delComment(id)
  ElNotification({
    title: '温馨提示',
    message: '删除成功',
    type: 'success',
    duration: 1500
  })
  getComment()
}

onMounted(() => {
  getComment()
})
</script>

<template>
  <page-container title="评论管理">
    <!-- 表单 -->
    <el-form style="display: flex">
      <el-form-item label="关键词：">
        <el-input
          v-model="params.key"
          placeholder="请输入关键词"
          style="width: 240px; margin-right: 20px"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="searchBtn">搜索</el-button>
        <el-button @click="resetBtn">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table
      :data="commentData"
      v-loading="loading"
      style="width: 100%"
      :row-style="{ height: '90px' }"
    >
      <el-table-column type="index" label="序号" width="100" align="center" />
      <el-table-column label="内容" align="center">
        <template #default="{ row }">
          <span class="text">{{ row.content }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发表人" align="center">
        <template #default="{ row }">
          <span>{{ row.nickname || row.username }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="time" label="发表时间" align="center" />
      <el-table-column label="操作" align="center">
        <template #default="{ row }">
          <el-button type="primary" link @click="selectDetail(row)"
            >查看</el-button
          >

          <el-button type="primary" link @click="delBtn(row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
      <!-- 暂无数据 -->
      <template #empty>
        <el-empty description="暂无数据" />
      </template>
    </el-table>

    <!-- 分页 -->
    <el-row
      style="display: flex; justify-content: end"
      v-if="commentData.length > 0"
    >
      <el-pagination
        v-model:current-page="params.page"
        v-model:page-size="params.pageSize"
        :page-sizes="[2, 3, 5, 10]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        style="margin-top: 20px"
      />
    </el-row>
  </page-container>

  <!-- 评论详情Dialog弹层 -->
  <el-dialog v-model="isShowCommentDialog" title="评论详情" width="666">
    <el-form :model="commentDetail" label-width="50px" style="padding: 10px">
      <el-form-item label="内容" label-width="60">
        <el-input
          v-model="commentDetail.content"
          disabled
          type="textarea"
          autosize
        ></el-input>
      </el-form-item>
      <el-form-item label="发表人" label-width="60">
        <el-input
          v-model="commentDetail.nickname"
          disabled
          v-if="commentDetail.nickname"
        ></el-input>
        <el-input v-model="commentDetail.username" disabled v-else></el-input>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<style scoped lang="scss">
.text {
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.img {
  height: 100%;
  border-radius: 8px;
}
</style>
